﻿$_M = new Object();
$_M._variableName = '$_M';
$_M.LoadingUrl = null;
$_M.wCss = new Array();
$_M.wTitleCss = new Array();
$_M.wCloseCss = new Array();
$_M.wConCss = new Array();
$_M.wMaskCss = new Array();
$_M.wFootCss = new Array();
$_M.wResizeCss = new Array();
$_M.ZIndex = 100;
$_M._isShown = false;
$_M._initialized = false;
$_M._mod = null;
$_M._mTitle = null;
$_M._mClose = null;
$_M._mAniMask = null;
$_M._mMask = null;
$_M._mIframe = null;
$_M._mResize = null;
$_M._mFoot = null;
$_M._mCont = null;
$_M._aniHandle = null;
$_M._isOpen = false;
$_M._hidSel = null;
$_M._ckScrHan = null;
$_M._lModInfo = null;
$_M._lWinInfo = null;
$_M._isDrag = false;
$_M._moveModInfo = null;
$_M._resModInfo = null;
$_M._isResizing = false;

var de = document.documentElement;
$_M.Configure = function(LoadingUrl, wCss, wTitleCss, wCloseCss, wConCss, wFootCss, wResizeCss, wMaskCss, zIndex)
{
	this.LoadingUrl = LoadingUrl;
	this.wCss = wCss;
	this.wTitleCss = wTitleCss;
	this.wCloseCss = wCloseCss;
	this.wConCss = wConCss;
	this.wMaskCss = wMaskCss;
	this.wFootCss = wFootCss;
	this.wResizeCss = wResizeCss;
	this.ZIndex = zIndex;
};

$_M.IsShown = function(){return this._isShown;};

$_M._getWinInfo = function()
{
	var scrollX = 0, scrollY = 0, width = 0, height = 0, cWidth = 0, cHeight = 0;

	if (typeof(window.pageXOffset) == 'number') 
	{
		scrollX = window.pageXOffset;
		scrollY = window.pageYOffset;
	} 
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) 
	{
		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;
	} 
	else if (de && (de.scrollLeft || de.scrollTop)) 
	{
		scrollX = de.scrollLeft;
		scrollY = de.scrollTop;
	}
	
	if (typeof(window.innerWidth) == 'number') 
	{
		width = window.innerWidth;
		height = window.innerHeight;
	} 
	else if (de && (de.clientWidth || de.clientHeight)) 
	{
		width = de.clientWidth;
		height = de.clientHeight;
	} 
	else if (document.body && (document.body.clientWidth || document.body.clientHeight)) 
	{
		width = document.body.clientWidth;
		height = document.body.clientHeight;
	}
	
	if (de && (de.scrollHeight || de.offsetHeight))
	{
		if (de.scrollHeight > de.offsetHeight)
		{
			cWidth = de.scrollWidth;
			cHeight = de.scrollHeight;
		}
		else
		{
			cWidth = de.offsetWidth;
			cHeight = de.offsetHeight;
		}
	}
	else if (document.body && (document.body.scrollHeight || document.body.offsetHeight))
	{
		if (document.body.scrollHeight > document.body.offsetHeight)
		{
			cWidth = document.body.scrollWidth;
			cHeight = document.body.scrollHeight;
		}
		else
		{
			cWidth = document.body.offsetWidth;
			cHeight = document.body.offsetHeight;
		}
	}
	else
	{
		cWidth = width;
		cHeight = height;
	}
	
	if (height > cHeight)	height = cHeight;		
	if (width > cWidth) width = cWidth;	
	cWidth = cWidth - 20;	
	var rect = new Object();
	rect.ScrollX = scrollX;
	rect.ScrollY = scrollY;
	rect.Width = width;
	rect.Height = height;
	rect.cWidth = cWidth;
	rect.cHeight = cHeight;
	
	return rect;
};

$_M._getCurStyVal = function(element, styleRule, jsStyleRule)
{
	var value = 0;	
	if(document.defaultView && document.defaultView.getComputedStyle) value = parseInt(document.defaultView.getComputedStyle(element, "").getPropertyValue(styleRule), 0); else if(element.currentStyle) value = parseInt(element.currentStyle[jsStyleRule], 0);
	if (!isNaN(value)) return value; else return 0;
};

$_M._calStyleOffset = function(element)
{
	var result = new Object();	
	result.Height = this._getCurStyVal(element, 'border-top-width', 'borderTopWidth') + 
		this._getCurStyVal(element, 'border-bottom-width', 'borderBottomWidth') +
		this._getCurStyVal(element, 'padding-top', 'paddingTop') +
		this._getCurStyVal(element, 'padding-bottom', 'paddingBottom');	
	result.Width = this._getCurStyVal(element, 'border-left-width', 'borderLeftWidth') + 
		this._getCurStyVal(element, 'border-right-width', 'borderRightWidth') +
		this._getCurStyVal(element, 'padding-left', 'paddingLeft') +
		this._getCurStyVal(element, 'padding-right', 'paddingRight');	
	return result;
};

$_M.Open = function (url, width, height, onCloseFunction, x, y, ignoreClose)
{
	if (!ignoreClose && this._isShown) this.Close();
	else if (this._hidSel)
	{
		for (var i = 0; i < this._hidSel.length; i++)
		{
			if (this._hidSel[i].Element.style.visibility == 'hidden')
				this._hidSel[i].Element.style.visibility = this._hidSel[i].Visibility;
		}			
		this._hidSel = null;
	}
	
	if (!this._initialized)	this._initialize();	
	try	{ this._mTitle.childNodes[1].innerHTML = this._mIframe.contentWindow.document.title; } catch (err){}
	
	if (!ignoreClose) this._mIframe.src = url;	
	try	{ this._mIframe.contentWindow.opener = window; } catch (err){}
	
	this._mAniMask.style.display = 'none';
	this._mMask.style.display = 'none';
	
	this._lWinInfo = this._getWinInfo();	
	this._mAniMask.style.display = 'block';
	
	if (width > this._lWinInfo.Width) width = this._lWinInfo.Width;
	
	this._mAniMask.style.position = 'absolute';
	this._mAniMask.style.zIndex = this.ZIndex;
	this._mAniMask.style.display = 'block';
	this._mAniMask.style.visibility = 'hidden';
	this._mAniMask.style.overflow = 'hidden';
	
	this._mAniMask.style.width = width + 'px';
	this._mCont.style.width = width + 'px';
	
	this._mod.style.position = 'absolute';
	this._mod.style.display = 'block';
	this._mod.style.visibility = 'hidden';
	this._mod.style.left = '0px';
	this._mod.style.top = '0px';
	
	this._mMask.style.position = 'absolute';
	this._mMask.style.display = 'block';
	this._mMask.style.zIndex = this.ZIndex;
	this._mMask.style.visibility = 'visible';
	
	var modalContentOffset = this._calStyleOffset(this._mCont);
	
	var offset = (this._mod.offsetHeight - this._mCont.offsetHeight) - modalContentOffset.Height;
	if (height + offset > this._lWinInfo.Height)height = this._lWinInfo.Height - offset;		
	if (width < this._mResize.offsetWidth * 2)width = this._mResize.offsetWidth * 2;	
	if (width < this._mClose.offsetWidth * 2)width = this._mClose.offsetWidth * 2;	
	if (height < this._mTitle.offsetHeight + this._mFoot.offsetHeight)height = this._mTitle.offsetHeight + this._mFoot.offsetHeight;
		
	this._mIframe.style.height = height + 'px';
	this._mCont.style.height = height + 'px';
	this._mCont.style.width = (width - (this._mod.offsetWidth - this._mCont.offsetWidth) - modalContentOffset.Width) + 'px';
	this._mAniMask.style.width = width + 'px';
	this._mAniMask.style.height = this._mod.offsetHeight + 'px';
	
	this._mMask.style.left = '0px';
	this._mMask.style.top = '0px';	
	this._mMask.style.width = this._lWinInfo.cWidth + 'px';
	this._mMask.style.height = this._lWinInfo.cHeight + 'px';
	
	this._lWinInfo = this._getWinInfo();
	
	var panelWidth = this._mod.offsetWidth;
	var panelHeight = this._mod.offsetHeight;
	var animatePropertyName, animateTargetValue, animateNextValue;
	
	if (typeof(x) == 'undefined' || isNaN(parseInt(x, 10)))	x = ((this._lWinInfo.Width - panelWidth) / 2) + this._lWinInfo.ScrollX;	
	if (x + panelWidth > this._lWinInfo.Width + this._lWinInfo.ScrollX)	x = this._lWinInfo.Width + this._lWinInfo.ScrollX - panelWidth;		
	if (x < this._lWinInfo.ScrollX)x = this._lWinInfo.ScrollX;	
	if (typeof(y) == 'undefined' || isNaN(parseInt(y, 10)))	y = ((this._lWinInfo.Height - panelHeight) / 2) + this._lWinInfo.ScrollY;	
	if (y + panelHeight > this._lWinInfo.Height + this._lWinInfo.ScrollY)y = this._lWinInfo.Height + this._lWinInfo.ScrollY - panelHeight;	
	if (y < this._lWinInfo.ScrollY)y = this._lWinInfo.ScrollY;		
	this._mAniMask.style.left = x + 'px';
	this._mAniMask.style.top = y + 'px';
	
	animateTargetValue = 0;
	animateNextValue = -panelHeight;
	
	this._mod.style.visibility = 'visible';
	this._mAniMask.style.visibility = 'visible';
	this._mAniMask.style.overflow = 'hidden';
	
	if (this._mAniMask.getClientRects)
	{
		var selectBoxes = document.getElementsByTagName('select');
		this._hidSel = new Array();
		for (var i = 0; i < selectBoxes.length; i++)
		{
			this._hidSel[this._hidSel.length] = { Element: selectBoxes[i], Visibility: selectBoxes[i].style.visibility };
			selectBoxes[i].style.visibility = 'hidden';
		}
	}
	
	this._isOpen = true;
	if (ignoreClose)
		this._aniHandle = window.setTimeout(new Function(this._variableName + '._animate(0,0,0,0);'), 9);
	else
	{
		this._mIframe.style.display = 'none';
		this._animate(0, -panelHeight, panelHeight / 3, .67);
	}

	this._lModInfo = { Url : this._mIframe.src, OnCloseFunction : onCloseFunction, X : x, Y : y, Width : parseInt(width, 10), Height: parseInt(height, 10) };	
	this._isShown = true;
};

$_M._checkScrRes = function()
{
	if (this._ckScrHan) window.clearTimeout(this._ckScrHan);

	if (this._isShown && !this._isOpen && this._lWinInfo)
	{
		try
		{
			this._mTitle.childNodes[1].innerHTML = this._mIframe.contentWindow.document.title;
		}
		catch (err)
		{
		}
		
		var windowInfo = this._getWinInfo();
		if (windowInfo.ScrollX != this._lWinInfo.ScrollX || windowInfo.ScrollY != this._lWinInfo.ScrollY || windowInfo.Width != this._lWinInfo.Width || windowInfo.Height != this._lWinInfo.Height)
			this.Open(null, this._lModInfo.Width, this._lModInfo.Height, this._lModInfo.OnCloseFunction, this._lModInfo.X, this._lModInfo.Y, true);
		else
			this._ckScrHan = window.setTimeout(new Function('window.' + this._variableName + '._checkScrRes();'), 999);
	}
};

$_M.Close = function(returnValue)
{
	if (this._isShown)
	{
		if (!this._initialized) this._initialize();	
		this._mod.style.position = 'absolute';
		this._mod.style.display = 'none';
		this._mAniMask.style.position = 'absolute';
		this._mAniMask.style.display = 'none';
		this._mMask.style.position = 'absolute';
		this._mMask.style.display = 'none';
		this._mIframe.src = this.LoadingUrl;
		var onCloseFunction = this._lModInfo.OnCloseFunction;		
		this._isShown = false;
		this._lModInfo = null;
		this._windowInfo = null;
		
		if (this._hidSel)
		{
			for (var i = 0; i < this._hidSel.length; i++){if (this._hidSel[i].Element.style.visibility == 'hidden') this._hidSel[i].Element.style.visibility = this._hidSel[i].Visibility;}				
			this._hidSel = null;
		}
		if (onCloseFunction) onCloseFunction(returnValue);		
		this.Dispose();
	}
};

$_M.Refresh = function()
{
	if (this._aniHandle) window.clearTimeout(this._aniHandle);	
	this.Dispose();	
	if (this._isShown && this._lModInfo)	this.Open(this._lModInfo.Url, this._lModInfo.Width, this._lModInfo.OnCloseFunction, this._lModInfo.Height, this._lModInfo.OnCloseFunction, this._lModInfo.X, this._lModInfo.Y);
};

$_M._initialize = function()
{
	this._mMask = document.createElement('div');
	this._mMask.style.width = 'auto';
	this._mMask.style.height = 'auto';
	this._mMask.style.position = 'absolute';
	this._mMask.style.display = 'none';
	var mm = this._mMask;	
	if (this.wMaskCss.length > 0)
	{
		mm.className = this.wMaskCss[0];
		for (var i = 1; i < this.wMaskCss.length; i++)
		{
			mm.appendChild(document.createElement('div'));
			mm = mm.childNodes[0];
			mm.className = this.wMaskCss[i];
			mm.style.width = 'auto';
			mm.style.height = 'auto';
		}
	}	
	document.body.appendChild(this._mMask);	
	this._mAniMask = document.createElement('div');
	this._mAniMask.style.position = 'absolute';
	this._mAniMask.style.display = 'none';
	this._mAniMask.style.overflow = 'hidden';	
	this._mod = document.createElement('div');
	this._mod.style.width = 'auto';
	this._mod.style.height = 'auto';
	this._mod.style.position = 'absolute';
	this._mod.style.display = 'none';
	var m = this._mod;	
	if (this.wCss.length > 0)
	{
		m.className = this.wCss[0];
		for (var i = 1; i < this.wCss.length; i++)
		{
			m.appendChild(document.createElement('div'));
			m = m.childNodes[0];
			m.className = this.wCss[i];
			m.style.width = 'auto';
			m.style.height = 'auto';
		}
	}
	
	this._mTitle = document.createElement('div');
	m.appendChild(this._mTitle);
	if (this.wTitleCss.length > 0)
	{
		this._mTitle.className = this.wTitleCss[0];
		for (var i = 1; i < this.wTitleCss.length; i++)
		{
			this._mTitle.appendChild(document.createElement('div'));
			this._mTitle = this._mTitle.childNodes[0];
			this._mTitle.className = this.wTitleCss[i];
		}
	}
	this._mTitle.onmousedown = new Function('event', 'window.' + this._variableName + '._startDrag(event); return false;');	
	this._mClose = document.createElement('div');
	this._mTitle.appendChild(this._mClose);	
	var mc = this._mClose;
	if (this.wCloseCss.length > 0)
	{
		mc.className = this.wCloseCss[0];
		for (var i = 1; i < this.wCloseCss.length; i++)
		{
			mc.appendChild(document.createElement('div'));
			mc = mc.childNodes[0];
			mc.className = this.wCloseCss[i];
		}
	}	
	this._mClose.onclick = new Function('window.' + this._variableName + '.Close();');		
	this._mTitle.appendChild(document.createElement('span'));	
	var e = document.createElement('div');
	e.style.clear = 'both';
	this._mTitle.appendChild(e);	
	this._mCont = document.createElement('div');
	m.appendChild(this._mCont);
	if (this.wConCss.length > 0)
	{
		this._mCont.className = this.wConCss[0];
		for (var i = 1; i < this.wConCss.length; i++)
		{
			this._mCont.appendChild(document.createElement('div'));
			this._mCont = this._mCont.childNodes[0];
			this._mCont.className = this.wConCss[i];
		}
	}	
	this._mIframe = document.createElement('iframe');
	this._mIframe.src = this.LoadingUrl;
	this._mIframe.width = '100%';
	this._mIframe.border = '0';
	this._mIframe.frameBorder = 'no';
	this._mIframe.style.borderLeftWidth = '0px';
	this._mIframe.style.borderRightWidth = '0px';
	this._mIframe.style.borderTopWidth = '0px';
	this._mIframe.style.borderBottomWidth = '0px';
	this._mCont.appendChild(this._mIframe);
	
	this._mFoot = document.createElement('div');
	m.appendChild(this._mFoot);
	var mf = this._mFoot;
	if (this.wFootCss.length > 0)
	{
		mf.className = this.wFootCss[0];
		for (var i = 1; i < this.wFootCss.length; i++)
		{
			mf.appendChild(document.createElement('div'));
			mf = mf.childNodes[0];
			mf.className = this.wFootCss[i];
		}
	}
	this._mResize = document.createElement('div');
	mf.appendChild(this._mResize);
	var e = document.createElement('div');
	e.style.clear = 'both';
	mf.appendChild(e);
	var mr = this._mResize;
	if (this.wResizeCss.length > 0)
	{
		mr.className = this.wResizeCss[0];
		for (var i = 1; i < this.wResizeCss.length; i++)
		{
			mr.appendChild(document.createElement('div'));
			mr = mr.childNodes[0];
			mr.className = this.wResizeCss[i];
		}
	}	
	this._mResize.onmousedown = new Function('event', 'window.' + this._variableName + '._startResize(event); return false;');
	this._mAniMask.appendChild(this._mod);
	document.body.appendChild(this._mAniMask);	
	this._initialized = true;
};

$_M.Dispose = function()
{
	if (this._initialized)
	{
		if (this._aniHandle) window.clearTimeout(this._aniHandle);		
		this._isShown = false;
		this._isOpen = false;		
		if (document && document.body)
		{
			document.body.removeChild(this._mAniMask);
			document.body.removeChild(this._mMask);
			this._mClose.onclick = null;
			this._mTitle.onmousedown = null;
			this._mResize.onmousedown = null;
			this._mod = null;
			this._mTitle = null;
			this._mClose = null;
			this._mAniMask = null;
			this._mMask = null;
			this._mIframe = null;
			this._mResize = null;
			this._mFoot = null;
			this._mCont = null;
		}		
		this._initialized = false;
	}
};

$_M._animate = function(targetValue, nextValue, step, acceleration)
{
	if (this._aniHandle)window.clearTimeout(this._aniHandle);	
	if (!this._isOpen) return;			
	var currValue = parseInt(this._mod.style.top, 10);
	if ((step < 0 && currValue < targetValue) || (step > 0 && currValue > targetValue) || Math.abs(step) < 1)
	{
		if (this._hidSel){for (var i = 0; i < this._hidSel.length; i++) this._hidSel[i].Element.style.visibility = 'hidden';}	
		this._mod.style.top = targetValue + 'px';
		this._mod.style.position = 'static';
		this._mAniMask.style.overflow = 'visible';
		this._aniHandle = null;		
		if (!this._isResizing && !this._isDrag)	this._mIframe.style.display = 'block';		
		this._isOpen = false;		
		this._lWinInfo = this._getWinInfo();
		this._ckScrHan = window.setTimeout(new Function('window.' + this._variableName + '._checkScrRes();'), 999);
	}
	else
	{
		this._mod.style.top = nextValue + 'px';		
		nextValue = nextValue + step;
		if (step > 0 && nextValue > targetValue) nextValue = targetValue; else if (step < 0 && nextValue < targetValue)	nextValue = targetValue;		
		step = step * acceleration;		
		this._aniHandle = window.setTimeout(new Function(this._variableName + '._animate(' + targetValue + ',' + nextValue + ',' + step + ',' + acceleration + ');'), 19);
	}
};

$_M._startDrag = function(event)
{
	if (!this._initialized)	this._initialize();	
	if (!event)	event = window.event;	
	this._moveModInfo = new Object();		
	this._moveModInfo.StartMouseX = event.pageX ? event.pageX : event.screenX;
	this._moveModInfo.StartMouseY = event.pageY ? event.pageY : event.screenY;
	this._moveModInfo.StartModalX = this._lModInfo.X;
	this._moveModInfo.StartModalY = this._lModInfo.Y;
	this._moveModInfo.Button = event.button;	
	document.onmouseup = new Function('event', 'window.' + this._variableName + '._endDrag(event); return false;');
	document.onmousemove = new Function('event', 'window.' + this._variableName + '._drag(event); return false;');	
	this._mIframe.style.display = 'none';	
	this._isDrag = true;
};

$_M._endDrag = function(event)
{
	if (!this._initialized)	this._initialize();	
	this._isDrag = false;
	this._moveModInfo = null;
	document.onmouseup = null;
	document.onmousemove = null;
	this._mIframe.style.display = 'block';
};

$_M._drag = function(event)
{
	if (!this._initialized)	this._initialize();
	if (!event)	event = window.event;	
	if (event.button != this._moveModInfo.Button){this._endDrag(event); return;}
	var eventX = typeof(event.pageX) != 'undefined' ? event.pageX : event.screenX;
	var eventY = typeof(event.pageY) != 'undefined' ? event.pageY : event.screenY;	
	var xChange = eventX - this._moveModInfo.StartMouseX;
	var yChange = eventY - this._moveModInfo.StartMouseY;	
	this.Open(null, this._lModInfo.Width, this._lModInfo.Height, this._lModInfo.OnCloseFunction, this._moveModInfo.StartModalX + xChange, this._moveModInfo.StartModalY + yChange, true);
};

$_M._startResize = function(event)
{
	if (!this._initialized)	this._initialize();	
	if (!event)	event = window.event;	
	this._resModInfo = new Object();		
	this._resModInfo.StartMouseX = event.pageX ? event.pageX : event.screenX;
	this._resModInfo.StartMouseY = event.pageY ? event.pageY : event.screenY;
	this._resModInfo.StartModalWidth = this._lModInfo.Width;
	this._resModInfo.StartModalHeight = this._lModInfo.Height;
	this._resModInfo.Button = event.button;	
	document.onmouseup = new Function('event', 'window.' + this._variableName + '._endResize(event); return false;');
	document.onmousemove = new Function('event', 'window.' + this._variableName + '._resize(event); return false;');	
	this._mIframe.style.display = 'none';	
	this._isResizing = true;
};

$_M._endResize = function(event)
{
	if (!this._initialized)	this._initialize();	
	this._isResizing = false;
	this._resModInfo = null;
	document.onmouseup = null;
	document.onmousemove = null;
	this._mIframe.style.display = 'block';
};

$_M._resize = function(event)
{
	if (!this._initialized)	this._initialize();
	if (!event)	event = window.event;	
	if (event.button != this._resModInfo.Button){this._endResize(event);	return;	}
	var eventX = typeof(event.pageX) != 'undefined' ? event.pageX : event.screenX;
	var eventY = typeof(event.pageY) != 'undefined' ? event.pageY : event.screenY;	
	var xChange = eventX - this._resModInfo.StartMouseX;
	var yChange = eventY - this._resModInfo.StartMouseY;	
	this.Open(null, this._resModInfo.StartModalWidth + xChange, this._resModInfo.StartModalHeight + yChange, this._lModInfo.OnCloseFunction, this._lModInfo.X, this._lModInfo.Y, true);
};

$_M.Configure('/loading.html',['CommonModal'],['CommonModalTitle'],['CommonModalClose'],['CommonModalContent'],['CommonModalFooter'],['CommonModalResize'],['CommonModalMask'],100);